const fs = require("fs");
const http = require("http");

http
  .createServer((request, response) => {
    const { url, method, headers } = request;
    if (url === "/" && method === "GET") {
      fs.readFile("index.html", (err, data) => {
        if (err) {
          response.writeHead(500, {
            "Content-Type": "text/plian;charset=utf-8",
          });
          response.end("500 服务器挂了，兄弟！！！");
        }
        response.statusCode = 200;
        response.setHeader("Content-Typr", "text/html");
        response.end(data);
      });
    } else if (method === "GET" && headers.accept.indexOf("image/*" !== -1)) {
      fs.createReadStream("." + url).pipe(response);
    } else {
      response.statusCode = 400;
      response.setHeader("Content-Type", "text/plain;charste=utf-8");
      response.end("404 找不到了呀！");
    }
  })
  .listen(3000, () => {
    console.log("Server at http://localhost:3000/");
  });
